package com.kobeliu.controller;


import com.kobeliu.controller.request.LinkGroupAddRequest;
import com.kobeliu.controller.request.LinkGroupUpdateRequest;
import com.kobeliu.enums.BizCodeEnum;
import com.kobeliu.model.LinkGroupDO;
import com.kobeliu.service.LinkGroupService;
import com.kobeliu.util.JsonData;
import com.kobeliu.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kobeliu
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    /**
     * 创建分组
     * @param addRequest
     * @return
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest addRequest){

        int rows = linkGroupService.add(addRequest);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("/del/{group_id}")
    public JsonData del(@PathVariable("group_id")Long groupId){

        int rows = linkGroupService.del(groupId);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_DEL_FAIL);
    }

    /**
     * 分组详情
     * @param groupId
     * @return
     */
    @GetMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id")Long groupId){

        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    /**
     * 查询全部分组
     * @return
     */
    @GetMapping("/findAll")
    public JsonData findAll(){

        List<LinkGroupVO> linkGroupVOs = linkGroupService.findAll();
        return JsonData.buildSuccess();
    }

    @PostMapping("update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request){

        int rows = linkGroupService.update(request);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_UPDATE_FAIL);

    }

}

